package Util;

import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class jFrame4 extends JFrame{
	JPanel smainpanel;
	JPanel spanel1;
	JPanel spanel2;
	JPanel spanel3;
	JPanel spanel4;
	JButton sseachButton;
	JButton sButton6;
	JButton sButton7;
	JTextField outField1;
	JTextField outField2;
	JTextField outField3;
	JTextField outField4;
	JLabel mylabel;
	public jFrame4(){
		LayoutManager  bookview = new GridLayout(4,1);
        setLayout(bookview);
        setTitle("书店销售系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(400, 400, 485, 343);
		setLocationRelativeTo(null);
		smainpanel= new JPanel(new GridLayout(4,1));
		spanel1 = new JPanel();
		spanel2 = new JPanel();
		spanel3 = new JPanel();
		spanel4 = new JPanel();
		sseachButton = new JButton("销售信息");
		mylabel = new JLabel("输入顺序为顾客账号，书籍账号，销售日期，销售数目");
		sseachButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                String sql="select * from  sale ";
	                ResultSet rs=st.executeQuery(sql);
	                while(rs.next()) {
	    				String cnum =rs.getString("cnum");
	    				String  bnum=rs.getString("bnum");
	    				String  saladate =rs.getString("saladate");
	    				String   saleamount =rs.getString("saleamount");
	    				
	    				System.out.println(cnum+"--"+bnum+"--"+saladate+"--"+saleamount);
	    			}




	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	            }
				
			}
				
			
			
		});
		sButton6 = new JButton("卖出");
		sButton6.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                String cnum =  outField1.getText();
	                String bnum= outField2.getText();
	                String saledate= outField3.getText();
	                String saleamount = outField4.getText();
	                String sql= "insert into sale values(?,?,?,?)";
	                PreparedStatement ps = null;
	                ps=conn.prepareStatement(sql);
	                ps.setString(1, cnum);
	                ps.setString(2, bnum);
	                ps.setString(3, saledate);
	                ps.setString(4, saleamount);
	                ResultSet rs=ps.executeQuery();
	                JOptionPane.showMessageDialog(null,"售卖成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);





	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	            }
			}
			
		});
		outField1 = new JTextField(8);
		outField2 = new JTextField(8);
		outField3 = new JTextField(8);
		outField4 = new JTextField(8);
		spanel2.add(outField1);
		spanel2.add(outField2);
		spanel2.add(outField3);
		spanel2.add(outField4);
		spanel2.add(mylabel);
		spanel1.add(sseachButton);
		spanel2.add(sButton6);
		add(spanel2);
		add(spanel1);
		setVisible(true);
		
		}
	}


